/* ===== register =====：注册页 */
myApp.onPageInit('register', function (page) {
	console.log('register');
    countrycodelist = [];
    //国家码
    $$.getJSON(url_get_countrycode, {}, function (ajaxresult) {
        if(ajaxresult.code==1){
            var data_countrycode = ajaxresult.data;
            for(var i=0;i<data_countrycode.length;i++){
                countrycodelist.push(data_countrycode[i].country+"+"+data_countrycode[i].areaCode);
            }
            var pickerDevice = myApp.picker({
                input: '#register input[name="city"]',
                cols: [
                    {
                        textAlign: 'center',
                        values: countrycodelist
                    }
                ]
            });
        }else{
            ajaxfail(ajaxresult.msg);
        }
    });

    // code
    $$(".get_code").on('click',function(){
        var city = $$('#register input[name="city"]').val();
        var areacode = city.split("+")[1];
        console.log(areacode);
        var tel = $$('#register input[name="tel"]').val();
        console.log(tel);
        if(city&&tel){
            $$.ajax({
                url:url_get_telcode, 
                method:"POST",
                data:{
                    utype:'TOURIST', //类型(TOURIST,GUIDER)
                    areaCode:areacode,// 区号
                    phoneNum:tel
                },
                dataType:'json',
                success:function (ajaxresult) {
                    //获取数据
                    if(ajaxresult.code==1){
                        myApp.alert("验证码已发送，请注意查收。");                   
                    }else{
                        ajaxfail(ajaxresult.msg);
                    }
                },
            });
        }else{
            myApp.alert("请将国家和手机号补充完整，谢谢");
        }        
    });
    
	$$('#register .tab .row a').click(function(){
		$$('#register .tab .col-50_active').removeClass('col-50_active');
		$$(this).parent().addClass('col-50_active');
	});
    //游客注册
	$$('#register .register_tourist_submit').click(function(){
        var city = $$('#register .register_tourist input[name="city"]').val();
        var areacode = city.split("+")[1];
        var tel = $$('#register .register_tourist input[name="tel"]').val();
        var pwd = $$('#register .register_tourist input[name="pwd"]').val();
        var re_password = $$('#register .register_tourist input[name="pwd_again"]').val();
        var msgcode = $$('#register .register_tourist input[name="msgcode"]').val();
        console.log(areacode,tel,pwd,re_password,msgcode);
        if(city&&tel&&pwd&&re_password&&msgcode){
            $$.ajax({
                url:url_post_touristreg,
                method:"POST",
                data:{
                    password:pwd,           //     密码
                    re_password:re_password, //   确认密码
                    areaCode:areacode,              // 邮箱 / areaCode 区号 phoneNum 手机号  （/前为邮箱注册 /后为手机注册）
                    phoneNum:tel,
                    securimage_code_value:msgcode,  //  验证码 /手机验证码            （/前为邮箱注册 /后为手机注册）
                    way:'mobile'            //注册方式 email/mobile
                },
                dataType:'json',
                success: function (ajaxresult) {
                    //获取数据
                    if(ajaxresult.code==1){
                        myApp.alert("恭喜，注册成功！");
                        mainView.router.load({'url':'login.html'});             
                    }else{
                        ajaxfail(ajaxresult.msg);
                    }
                },
            });
        }else{
            myApp.alert("请将信息补充完整！");
        }                  
	});
    //玩伴注册
	$$('#register .register_parnter_submit').click(function(){
        var xing = $$('#register .register_parnter input[name="firstname"]').val();
        var ming = $$('#register .register_parnter input[name="secondname"]').val();
        var sex = $$('#register .register_parnter select[name="sex"]').val();
        var email = $$('#register .register_parnter input[name="email"]').val();
        var city = $$('#register .register_parnter input[name="city"]').val();
        var areacode = city.split("+")[1];
        var tel = $$('#register .register_parnter input[name="tel"]').val();
        var pwd = $$('#register .register_parnter input[name="pwd"]').val();
        var re_password = $$('#register .register_parnter input[name="pwd_again"]').val();
        var msgcode = $$('#register .register_parnter input[name="msgcode"]').val();
        console.log(xing,ming,sex,city,email,pwd,re_password);
        $$.ajax({
            url:url_post_partnerreg,
            method:"POST",
            data:{
                password:pwd,  //      密码
                re_password:re_password,//   确认密码
                email:email,//         邮箱 
                areaCode:areacode,// 区号 
                phoneNum:tel,// 手机号  
                securimage_code_value:msgcode,//  手机验证码
                xing:xing,// 姓
                ming:ming,// 名
                sex:sex//m,f 性别
            },
            dataType:'json',
            success: function (ajaxresult) {
                //获取数据
                if(ajaxresult.code==1){
                    myApp("恭喜，注册成功！");
                    mainView.router.load({'url':'login.html'})
                }else{
                    ajaxfail(ajaxresult.msg);
                }
            },
        });      
	});
});

/* ===== register_email =====：邮箱注册页  验证码！！！！！！！！！！！！！！！！！！！*/
myApp.onPageInit('register_email', function (page) {
	console.log('register_email');
    $$.getJSON(url_get_code, {}, function (ajaxresult) {
        //获取数据
        if(ajaxresult.code==1){
            var data_QRcode = ajaxresult.data;
            $$("#register_email .QRcode").html(data_QRcode.image);            
        }else{
            ajaxfail(ajaxresult.msg);
        }
    }); 
	//更换验证码
    $$('#register_email .QRcode').on('click',function(){
        $$.getJSON(url_get_code, {}, function (ajaxresult) {
            //获取数据
            if(ajaxresult.code==1){
                var data_QRcode = ajaxresult.data;
                $$("#register_email .QRcode").html(data_QRcode.image);            
            }else{
                ajaxfail(ajaxresult.msg);
            }
        }); 
    })
    //submit
    $$('#register_email .register_email_submit').click(function(){
        var email = $$('#register_email input[name="email"]').val();
        var pwd = $$('#register_email input[name="pwd"]').val();
        var re_password = $$('#register_email input[name="pwd_again"]').val();
        var emailcode = $$('#register_email input[name="emailcode"]').val();
        console.log(email,pwd,re_password,emailcode);
        $$.ajax({
            url:url_post_touristreg,
            method:"POST",
            data:{
                password:pwd,           //     密码
                re_password:re_password, //   确认密码
                email:email,              // 邮箱 / areaCode 区号 phoneNum 手机号  （/前为邮箱注册 /后为手机注册）
                securimage_code_value:emailcode,  //  验证码 /手机验证码            （/前为邮箱注册 /后为手机注册）
                way:'email'            //注册方式 email/mobile
            },
            dataType:'json',
            success: function (ajaxresult) {
                //获取数据
                if(ajaxresult.code==1){
                    myApp("恭喜，注册成功！");
                    mainView.router.load({'url':'login.html'})
                }else{
                    ajaxfail(ajaxresult.msg);
                }
            },       
        });
    });
});